﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JobSiteStarterKit.BOL;
using JobSiteStarterKit.DAL;
using System.IO;

public partial class UserControls_jobseeker_jobseeker : System.Web.UI.UserControl
{
    DataSet industry = new DataSet();
    DataSet funtionalArea = new DataSet();
    DataSet state;
    protected string strUsername = String.Empty;

    private bool blnRegister;

    public bool IsRegister
    {
        get
        {
            return this.blnRegister;
        }
        set
        {
            this.blnRegister = value;
            RenderMe();
        }
    }
    
    private void RenderMe()
    {
        if (this.blnRegister == false)
        {
            trAccountDetails.Visible = false;
            trUserName.Visible = false;
            trPassword.Visible = false;
            trConfirmPassword.Visible = false;
            trCheckUserAvail.Visible = false;
            trEmail.Visible = false;
            btnsubmit.ImageUrl = "~/Images/Company update.png";
            
        }
        else
        {
            trCheckUserAvail.Visible = true;
            trAccountDetails.Visible = true;
            trUserName.Visible = true;
            trPassword.Visible = true ;
            trConfirmPassword.Visible = true ;
            trEmail.Visible = true;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //filling the Industry
        

            industry = Industry.SelectIndustry();
            drpIndustry.DataSource = industry;
            drpIndustry.DataTextField = "CategoryName";
            drpIndustry.DataValueField = "CategoryID";
            drpIndustry.DataBind();
            // filling Countries
            drpCountry.DataSource = JobSiteStarterKit.BOL.Country.GetCountries();
            drpCountry.DataTextField = "CountryName";
            drpCountry.DataValueField = "CountryID";
            drpCountry.SelectedIndex = 76;
            drpCountry.DataBind();

            //filling state IndiaWise
            if (drpCountry.SelectedValue == "76")
            {
                state = State.GetStates(76);
                drpLocation.DataSource = state;
                drpLocation.DataTextField = "StateName";
                drpLocation.DataValueField = "StateID";
                drpLocation.DataBind();
            }
            // Generating New Code
            //TSecurityCode1.GenNewCode();
            //Getting Qualification Details 
            drpHigherQualification.DataSource = JobSiteStarterKit.BOL.EducationLevel.GetEducationLevels();
            drpHigherQualification.DataTextField = "EducationLevelName";
            drpHigherQualification.DataValueField = "EducationLevelID";
            drpHigherQualification.DataBind();

            //Getting Funtional Area
            funtionalArea = Industry.fillfuntionalArea();
            drpfutionalarea.DataSource = funtionalArea;
            drpfutionalarea.DataTextField = "SubcategoryName";
            drpfutionalarea.DataValueField = "SubcategoryID";
            drpfutionalarea.DataBind();

            //if (!blnRegister == false)
            //{
            //    FillUserData(Context.User.Identity.Name);

            //}
            if (Session["SESS_JobSeekerName"] != null && Convert.ToString(Session["SESS_JobSeekerName"]) != String.Empty)
            {
                if (!blnRegister == false)
                {
                    FillUserData(Convert.ToString(Session["SESS_JobSeekerName"]));
                }
                else
                {
                    blnRegister = true;
                }
            }
           

        }

    }

    public void FillUserData(string username)
    {
        using (DBAccess db = new DBAccess())
        {
            string strprocName = "JobsDb_Jobseekers_selectUserAvailibility";
            db.CommandText = strprocName;
            db.AddParameter("@Username", username);
            System.Data.SqlClient.SqlDataReader reader = (System.Data.SqlClient.SqlDataReader) db.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                txtcity.Text = reader["JobSeekerCity"].ToString();
                txtname.Text = reader["JobSeekerName"].ToString();
                txtusername.Text = reader["JobSeekerUserName"].ToString(); // added on 02/1/20120 to handle attached resume name 
                strUsername = reader["JobSeekerUserName"].ToString();
                txtcontactno.Text = reader["JobSeekerPhone"].ToString();
                txtKeyskill.Text = reader["JobSeekerKeySkills"].ToString();
                txtResumeHeadline.Text = reader["JobSeekerResumeHeadlines"].ToString();
                string  country;
                string location;
                string funtionalarea;
                string qualification;
                string industry;
                string expInyear;
                string salInlakh;
                string salInthousand;
                string expInmonth;
                string jobseekermoney;


                country = reader["JobSeekerCountry"].ToString();
                location = reader["JobSeekerState"].ToString();
                funtionalarea = reader["JobSeekerFunctionalArea"].ToString();
                qualification = reader["EducationLevelID"].ToString();
                industry = reader["JobSeekerIndustry"].ToString();
                expInmonth = reader["JobSeekerTotalExpMonth"].ToString();
                expInyear = reader["JobSeekerTotalExpYear"].ToString();
                salInlakh = reader["JobSeekerAnnualSalaryLakh"].ToString();
                salInlakh = (salInlakh.IndexOf('.') > -1) ? salInlakh.Substring(0, salInlakh.IndexOf('.')) : salInlakh;
                salInthousand =reader["JobSeekerAnnualSalaryMonth"].ToString();
                salInthousand = (salInthousand.IndexOf('.') > -1) ? salInthousand.Substring(0, salInthousand.IndexOf('.')) : salInthousand;
                
                drpCountry.SelectedValue = country;
                               
                drpLocation.SelectedValue= location;
                
                drpfutionalarea.SelectedValue= funtionalarea;
                drpfutionalarea.SelectedItem.Value = funtionalarea;
                
                drpHigherQualification.SelectedValue = qualification;
                
                drpIndustry.SelectedValue = industry;
                
                drpLakhs.SelectedValue = salInlakh;
                
                drpMonths.SelectedValue = expInmonth;
                
                drpThousands.SelectedValue = salInthousand;
                
                drpyears.SelectedValue = expInyear;
                
            }
        }

    }    

    protected void drpfutionalarea_TextChanged(object sender, EventArgs e)
    {
        //if (drpfutionalarea.SelectedItem = "others")
        //{
        //    txtFuntionalAreaOther.Visible = true;

        //}
    }

    protected void btnsubmit_Click(object sender, ImageClickEventArgs e)
    {   
       
        string Password;
        string CPassword;
        string strerr = String.Empty;
        Password = txtpassword.Text;
        CPassword = txtconfirmpassword.Text;
        if (Password != CPassword)
        {
            strerr = "Check Password";
            return;
        }
        if (Chkterms.Checked == false)
        {
            errterms.Text = "Please check Terms And Condition";
            return;
        }
        //checking the file extension
        string strExt = "";
        strExt = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf('.') + 1);
        if (strExt.ToLower() == "doc" || strExt.ToLower() == "docx")
        {
            //Checking of file Size

            if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0)
            {
                int idx;
                string filestring;
                string attachment;
                string filepath;
                filestring = FileUpload1.PostedFile.FileName;
                idx = filestring.LastIndexOf("\\");
                if (blnRegister)
                {
                    attachment = txtusername.Text.Trim();
                }
                else
                {
                    //if (Session["SESS_JobSeekerUserName"] != null && Convert.ToString(Session["SESS_JobSeekerUserName"]) != String.Empty)
                    //{
                    //    //attachment = Context.User.Identity.Name;
                    //    attachment = Convert.ToString(Session["SESS_JobSeekerUserName"]);
                    //}
                    //else
                    //{
                    //    string datetime = System.DateTime.Now.ToShortDateString().Replace("/", "_");
                    //    attachment = "rozgarjobseeker";
                    //}
                    attachment = txtusername.Text.Trim();
                }

                if (FileUpload1.PostedFile.ContentLength < 1500000)
                {
                    if (checkFileExt(filestring))
                    {
                        filepath = "~/App_Data/resume/" + attachment + "." + strExt.ToLower(); 
                        FileUpload1.SaveAs(MapPath(filepath));
                    }
                }
                else
                {
                    spanUploadFileError.InnerText = "Upload File Must Be Less than 1.5 Mb";
                    return;
                }

            }
           
        }
        else
        {
            spanUploadFileError.InnerText = "Uploaded file must be either .doc or .docx extension.";
            return;
        }
      
      
      
        
      Jobseeker.jobseekerdata objdata = new Jobseeker.jobseekerdata();
        
        objdata = this.getJOBSeekerregndata();
        Jobseeker obj = new Jobseeker();
        //checkPassword(txtpassword.Text, txtconfirmpassword.Text);
        if (!blnRegister)
        {

            obj.Editjobseeker(objdata);
        }
        else
        {
            try
            {
                obj.savejobseeker(objdata);
                Common.SendEMAILVerificationMail(txtemail.Text.Trim(), txtusername.Text.Trim(), "2");
                Response.Redirect("success.aspx");
            }
            catch (MembershipCreateUserException ex)
            {
                string msg = Common.CreateUserError(ex.StatusCode);
                UserAvailability.Text = msg;

            }
        }
        
    }

    public Jobseeker.jobseekerdata getJOBSeekerregndata()
    {
        
        
        Jobseeker.jobseekerdata objdata = new Jobseeker.jobseekerdata();
        objdata.JobSeekerName = txtname.Text.Trim();
        objdata.JobSeekerUserName = txtusername.Text.Trim();
        objdata.JobSeekerAnnualSalaryLakh =Convert.ToInt16(drpLakhs.Text.Trim());
        objdata.JobSeekerAnnualSalaryMonth = Convert.ToInt16(drpThousands.Text.Trim());
        objdata.JobSeekerCountry = Convert.ToInt16(drpCountry.SelectedItem.Value);
        objdata.JobSeekerEmail = txtemail.Text.Trim();
        objdata.JobSeekerFunctionalArea = Convert.ToInt16(drpfutionalarea.SelectedItem.Value);
        objdata.JobSeekerIndustry = Convert.ToInt16(drpIndustry.SelectedItem.Value);
        objdata.JobSeekerKeySkills = txtKeyskill.Text.Trim();
        objdata.JobSeekerPhone = txtcontactno.Text.Trim();
        objdata.JobSeekerState = Convert.ToInt16(drpLocation.SelectedItem.Value);   //Changed by ajay
        objdata.JobSeekerEducationOthers = txtQualiOther.Text.Trim(); //Changed by ajay
        objdata.JobSeekerCity = txtcity.Text.Trim();
        objdata.JobSeekerlocationOthers = txtLocationOther.Text.Trim();
        objdata.JobSeekerResumeHeadlines = txtResumeHeadline.Text.Trim();
        objdata.JobSeekerEducationLevelID = Convert.ToInt32(drpHigherQualification.SelectedItem.Value .Trim());
        objdata.JobseekerfuntionOthers = txtFuntionalAreaOther.Text.Trim();
        objdata.JobseekerIndustryOthers = txtIndustryother.Text.Trim();
        objdata.JobSeekerlocationOthers = txtLocationOther.Text.Trim();
        objdata.JobSeekerTotalExpMonth = Convert.ToInt32(drpMonths.SelectedItem.Text.Trim());
        objdata.JobSeekerTotalExpYear = Convert.ToInt32(drpyears.SelectedItem.Text.Trim());
        if (rdodollar.Checked == true)
        {
            objdata.JobSeekerMoney = "Dollar";
        }
        else
            objdata.JobSeekerMoney = "Rupees";
        objdata.JobSeekerIndustry = Convert.ToInt16(drpIndustry.SelectedItem.Value);
        objdata.jobseekerpassword = txtconfirmpassword.Text.Trim();
        return objdata;
    }

    protected void drpLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(drpLocation.SelectedItem.Text =="other") 
        {
            txtLocationOther.Visible=true;
        }
        else
        {
            txtLocationOther.Visible=false;
        }
    }

    protected void drpfutionalarea_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpfutionalarea.SelectedItem.Text == "Other")
        {
            txtFuntionalAreaOther.Visible = true;
        }
        else
        {
            txtFuntionalAreaOther.Visible = false;
        }
    }

    protected void drpIndustry_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpIndustry.SelectedItem.Text == "other")
        {
            txtIndustryother.Visible = true;
        }
        else
        {
            txtIndustryother.Visible = false;
        }
    }

    protected void drpHigherQualification_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpHigherQualification.SelectedItem.Text == "other")
        {
            txtQualiOther.Visible = true;
        }
        else
        {
            txtQualiOther.Visible = false;
        }
    }

    protected void drpCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        
        if (drpCountry.SelectedValue == "76")
        {
            state = State.GetStates(76);
            drpLocation.DataSource = state;
            drpLocation.DataTextField = "StateName";
            drpLocation.DataValueField = "StateID";
            drpLocation.DataBind();
        }
        else
        {
            drpLocation.Visible = false;
            txtLocationOther.Visible = true;
        }
    }

    protected void drpLocation_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }

    protected void drpLocation_TextChanged(object sender, EventArgs e)
    {

    }

    protected void lnkUserAvailability_Click(object sender, EventArgs e)
    {
        //MembershipUserCollection col = Membership.FindUsersByName(txtusername.Text.Trim());
        
        //if (col.Count > 0)
        //     UserAvailability.Text = "notavailable";
        
        //else
        //    UserAvailability.Text = "Available";
        if (txtusername.Text.Trim() != String.Empty)
        {
            DBAccess db = new DBAccess();
            string strprocName = "JobsDb_Jobseekers_selectUserAvailibility";
            db.CommandText = strprocName;
            db.AddParameter("@Username", txtusername.Text.Trim());
            System.Data.SqlClient.SqlDataReader reader = (System.Data.SqlClient.SqlDataReader)db.ExecuteReader();
            if (reader.HasRows)
            {
                UserAvailability.Text = "notavailable";
            }
            else
            {
                UserAvailability.Text = "Available";
            }
        }
        else
        {
            UserAvailability.Text = "Please enter username";
        }
       
    }

    bool checkFileExt(string filename)
    {
        int idx;
        
        idx = filename.LastIndexOf(".");
        string attachment = filename.Substring(idx);
         switch (attachment.ToLower())
         {
             case ".doc":
                 return true;
             case ".docx":
                 return true;
             default:
                 return false;
         }
    }
    
}

